package com.tech.arno.spicy.mvp.presenterImpl;

import android.text.Html;
import android.widget.ListView;
import android.widget.TextView;

import com.tech.arno.spicy.db.database.DBDataHelper;
import com.tech.arno.spicy.db.database.DBHelper;
import com.tech.arno.spicy.module.BaseModel;
import com.tech.arno.spicy.module.search.ProductModel;
import com.tech.arno.spicy.mvp.presenter.MySearchPresenter;
import com.tech.arno.spicy.mvp.view.MySearchView;
import com.tech.arno.spicy.ui.adapter.SearchAdapter;

import java.util.ArrayList;

/*
*   @package:     com.tech.arno.spicy.mvp.presenterImpl
*   @fileName:    MySearchPresenterImpl
*   @author:    ArnoFrost
*   @date:  2017/12/22 0022 8:53
*   @desc:     搜索Presnter实现类
*/

public class MySearchPresenterImpl implements MySearchPresenter {
    public MySearchPresenterImpl(MySearchView searchView) {
        this.searchView = searchView;
    }

    MySearchView searchView;


    @Override
    public void searchText(String selections, ArrayList<BaseModel> searchingListData, SearchAdapter searchingAdapter, ListView searchingListView, TextView searchNoView) {
        if (selections != null && !selections.equals("")) {
            searchView.entrySearchMode();
            /**
             * 重数据库中模糊查询匹赔基金
             */
            searchingListData = DBDataHelper
                    .getInstance()
                    .select(DBHelper.FUND_LIST_TABLE,
                            "fdcode like ? or abbrev like ? or spell like ?",
                            new String[]{"%" + selections + "%",
                                    "%" + selections + "%",
                                    "%" + selections + "%"}, null,
                            ProductModel.class);

            if (searchingAdapter == null) {
//                searchingAdapter = new SearchAdapter(
//                        MySearchActivity.this, searchingListData);
                searchView.initAdapter();
                searchingListView.setAdapter(searchingAdapter);
            } else {
                searchingAdapter.updateData(searchingListData);
            }
            /**
             * 滚动到第零项
             */
            searchingListView.smoothScrollToPosition(0);
            searchNoView.setText(Html.fromHtml(searchView.getNoFundInfo(selections)));
        } else {
            searchView.decideWhichMode();
        }
    }

    @Override
    public void onDestroy() {
        searchView = null;

    }
}
